package cn.exrick.xboot.modules.research.controller;

import cn.exrick.xboot.common.utils.PageUtil;
import cn.exrick.xboot.common.utils.ResultUtil;
import cn.exrick.xboot.common.vo.PageVo;
import cn.exrick.xboot.common.vo.Result;
import cn.exrick.xboot.modules.research.entity.Achievement;
import cn.exrick.xboot.modules.research.service.IAchievementService;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * @author lty
 */
@Slf4j
@RestController
@Api(tags = "科研成果类型管理管理接口")
@RequestMapping("/xboot/achievement")
@Transactional
public class AchievementController {

    @Autowired
    private IAchievementService iAchievementService;

    @RequestMapping(value = "/get/{id}", method = RequestMethod.GET)
    @ApiOperation(value = "通过id获取")
    public Result<Achievement> get(@PathVariable String id) {

        Achievement achievement = iAchievementService.getById(id);
        return new ResultUtil<Achievement>().setData(achievement);
    }

    @RequestMapping(value = "/getAll", method = RequestMethod.GET)
    @ApiOperation(value = "获取全部数据")
    public Result<List<Achievement>> getAll() {

        List<Achievement> list = iAchievementService.list();
        return new ResultUtil<List<Achievement>>().setData(list);
    }

    @RequestMapping(value = "/getByPage", method = RequestMethod.GET)
    @ApiOperation(value = "分页获取")
    public Result<IPage<Achievement>> getByPage(@ModelAttribute Achievement achievement,@ModelAttribute PageVo page) {
        QueryWrapper<Achievement> qw =new QueryWrapper<>();

        if (StrUtil.isNotBlank(achievement.getAchievementId())){
            qw.like("achievement_Id",achievement.getAchievementId());
        }
        if (StrUtil.isNotBlank(achievement.getAchievementName())){
            qw.like("achievement_Name",achievement.getAchievementName());
        }
        if (StrUtil.isNotBlank(achievement.getUnit())){
            qw.like("unit",achievement.getUnit());
        }
        if (StrUtil.isNotBlank(achievement.getTime())){
            qw.eq("time",achievement.getTime());
        }

        IPage<Achievement> data = iAchievementService.page(PageUtil.initMpPage(page),qw);
        return new ResultUtil<IPage<Achievement>>().setData(data);
    }

    @RequestMapping(value = "/insertOrUpdate", method = RequestMethod.POST)
    @ApiOperation(value = "编辑或更新数据")
    public Result<Achievement> saveOrUpdate(Achievement achievement) {

        if (iAchievementService.saveOrUpdate(achievement)) {
            return new ResultUtil<Achievement>().setData(achievement);
        }
        return new ResultUtil<Achievement>().setErrorMsg("操作失败");
    }

    @RequestMapping(value = "/delByIds", method = RequestMethod.POST)
    @ApiOperation(value = "批量通过id删除")
    public Result<Object> delAllByIds(@RequestParam String[] ids) {

        for (String id : ids) {
            iAchievementService.removeById(id);
        }
        return ResultUtil.success("批量通过id删除数据成功");
    }

    @RequestMapping(value = "/audit", method = RequestMethod.POST)
    @ApiOperation(value = "审核")
    public Result<Object> audit(@RequestParam String id,@RequestParam int flag) {
        Achievement achievement = iAchievementService.getById(id);
        if(achievement == null) {
            return ResultUtil.error("科研成果不存在");
        }
        achievement.setStatus(flag);
        iAchievementService.saveOrUpdate(achievement);
        return ResultUtil.success("OK");
    }

    @RequestMapping(value = "/importDataApi",method = RequestMethod.POST)
    @ApiOperation(value = "导入数据")
    public Result<Object> importData(@RequestBody List<Achievement> achievements){

        List<Integer> errors = new ArrayList<>();
        List<String> reasons = new ArrayList<>();
        int count = 0;
        for (Achievement achievement : achievements) {
            count++;
            if (StrUtil.isBlank(achievement.getAchievementId())||StrUtil.isBlank(achievement.getAchievementName())){
                errors.add(count);
                reasons.add("数据为空值");
                continue;
            }

            iAchievementService.saveOrUpdate(achievement);
        }
        // 批量保存数据
        int successCount = achievements.size() - errors.size();
        String successMessage = "全部导入成功，共计 " + successCount + " 条数据";
        String failMessage = "导入成功 " + successCount + " 条，失败 " + errors.size() + " 条数据。<br>" +
                "第 " + errors.toString() + " 行数据导入出错，错误原因分别为：<br>" + reasons.toString();
        String message = "";
        if(errors.size()==0){
            message = successMessage;
        }else{
            message = failMessage;
        }
        return ResultUtil.success(message);
    }
}
